<template>
    <el-dialog title="创建物资信息" :visible.sync="dialogFormVisible" width="35%" :append-to-body="true"
    @close="dialogClose">
      <el-form status-icon label-width="100px">
          <el-form-item label="物资编号">
              <el-input v-model="materialinfo.fmid" type="text" autocomplete="off" />
          </el-form-item>
          <el-form-item label="物资名称">
              <el-input v-model="materialinfo.fmname" type="text" autocomplete="off" />
          </el-form-item>
          <el-form-item label="物资种类">
            <el-radio-group v-model="materialinfo.fmkind">
            <el-radio :label="1">设备</el-radio>
            <el-radio :label="2">耗材</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="品牌">
            <el-input v-model="materialinfo.fmbrand" type="text" autocomplete="off" />
          </el-form-item>
          <el-form-item label="型号">
              <el-input v-model="materialinfo.fmtype" type="text" autocomplete="off" />
          </el-form-item>
          <el-form-item label="主要参数">
              <el-input v-model="materialinfo.fmspec" type="text" autocomplete="off" />
          </el-form-item>
          <el-form-item label="计量单位">
            <el-input v-model="materialinfo.fmunit" type="text" autocomplete="off" />
          </el-form-item>
          <el-form-item label="详细描述">
            <el-input v-model="materialinfo.fmdesc" type="textarea" autocomplete="off" />
          </el-form-item>
          <el-form-item label="备注">
              <el-input v-model="materialinfo.fmmemo" type="textarea" placeholder="请输入内容" maxlength="200"
              show-word-limit  size="medium" autocomplete="off" />
          </el-form-item>
          <el-form-item>
              <el-button type="primary" @click="submitForm(materialinfo)">提交</el-button>
              <el-button @click="resetForm">重置</el-button>
          </el-form-item>
      </el-form>
    </el-dialog>
</template>
<script>
import { createMaterial,updateMaterial } from '@/api/material'

export default {
    props: {
        dialogFormVisible: Boolean,
        editMaterialInfo: {
            type:Array,
            default: []
        },
        updateOrCreate: Boolean
    },
    data() {
        return {
            materialinfo: {}
        }
    },
    updated () {
        this.materialinfo = this.editMaterialInfo
    },
    methods: {
        submitForm(materialinfo) {
            if (this.updateOrCreate) {
                updateMaterial(materialinfo).then(response => {
                    this.$message.success('修改物资信息成功')
                    this.$parent.fetchData()
                })
                this.dialogClose()
            } else {
                createMaterial(materialinfo).then(response => {
                    this.$message.success('创建物资信息成功')
                    this.$parent.fetchData()     
                })
                this.dialogClose()
            }
        },
        resetForm() {
            this.materialinfo = {}
        },
        // 解决父页面按钮只能点击一次问题
        dialogClose() {
            this.dialogFormVisible = false
            this.$parent.vueReset()
        }
    }
}
</script>
    